package com.eboy.widget;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.IBinder;
import android.widget.RemoteViews;

public class widgetService extends Service {
	private Timer timer;

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		timer = new Timer();
		timer.schedule(new task(), 0, 1000);
	}
	
	private final class task extends TimerTask{
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String time = dateFormat.format(new Date());
			RemoteViews views = new RemoteViews(getPackageName(), R.layout.example_appwidget);
			views.setTextViewText(R.id.textView, time);	
			AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
			appWidgetManager.updateAppWidget(
					new ComponentName(getApplicationContext(), widgetProvider.class), views);
		}		
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		timer.cancel();
		timer = null;
		super.onDestroy();		
	}
}